package ru.hh.school;

public class Util {
    /**
     *
     * @param args command line args
     * @return parsed port number or -1 if no port specified
     */
    public static int readPortFromAppArgs(String[] args) {
        if ((args.length == 0) || (args.length > 2)) {
            usage();
        }

        int port = -1;
        try {
            port = Integer.parseInt(args[0]);
            if (port < 0 || port > 65535) {
                usage();
            }
        } catch (NumberFormatException e) {
            usage();
        }
        return port;
    }

    public static String readServerHostnameFromAppArgs(String[] args) {
        if ((args.length == 0) || (args.length > 2)) {
            usage();
        }
        
        if (args.length == 1) {
            return null;
        } else {
            return args[1];
        }
    }


    public static void usage() {
        System.err.println("Should be started from bashscript");
    }
}
